<?php

namespace app\api\controller;

use app\admin\model\Price;
use app\admin\model\Goods;
use app\common\controller\Api;
use think\Config;
use think\Db;

/**
 * 首页接口
 */
class Task extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function _initialize(){
        parent::_initialize();
    }
    /**
     * 每日价格统计 23点10分统计
     * 查询到出售日期还未完成的订单
     * 未按时完成订单，扣除违约金，并修改订单状态
     */
    public function index()
    {
        $model = new \app\admin\model\Order();
        $list = $model
            ->with(['good'])
            ->where(['status'=>1])
            ->where('selltime','<=',date('Y-m-d',time()))
            ->order('id', 'desc')
            ->limit(5)
            ->select();
        $log = Price::find();
        Db::startTrans();
        try {
            foreach ($list as $v){
                //如果未发货，扣除卖家违约金给买家
                //如果已发货，扣除买家违约金给卖家
                $num = 1;
                if($v['title'] == '红壳洋鸡蛋'){
                    $price = $log['price'];
                    $num = 27.5;
                }elseif ($v['title'] == '粉壳草鸡蛋'){
                    $price = $log['pink_price'];
                    $num = 45;
                }elseif ($v['title'] == '小码虫草蛋'){
                    $price = $log['small_price'];
                    $num = 35;
                }
                if($v['price'] <= $price){
                    $dedit = Config::get('site.dedit');
                }else{
                    $dedit = bcmul(bcsub($v['price'],$price),($v['good']['num']/$num),2);
                }
                $model->earnestReturn($v,$dedit);
                \db('order')->where('id', $v['id'])->update([
                    'status' => 2
                ]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('请求成功');
    }

    /**
     * 到期未交易退还押金
     */
    public function goods()
    {
        $model = new \app\admin\model\Goods();
        $list = $model
            ->where(['status'=>1])
            ->where('selltime','<=',date('Y-m-d',time()))
            ->order('id', 'desc')
            ->limit(5)
            ->select();
        Db::startTrans();
        try {
            foreach ($list as $v){
                $model->earnestReturn($v);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('请求成功');
    }
    /**
     * 下架商品退还押金
     */
    public function outgoods()
    {
        $model = new \app\admin\model\Goods();
        $list = $model
            ->where(['status'=>2])
            ->order('id', 'desc')
            ->limit(5)
            ->select();
        Db::startTrans();
        try {
            foreach ($list as $v){
                $model->earnestReturn($v);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('请求成功');
    }
}
